package com.os.ui;

import javax.swing.*;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class ElementUITable extends JTable {

    public ElementUITable(DefaultTableModel model) {
        setModel(model);
        setShowGrid(true);
        setIntercellSpacing(new Dimension(0, 0)); // 设置单元格间距为0
        setFillsViewportHeight(true);
        setUI(new ElementUITableUI());
        setFont(new Font("Arial", Font.PLAIN, 14));
        setRowHeight(30);
        setGridColor(new Color(200, 200, 200));
        setSelectionBackground(new Color(230, 247, 255));
        setCellSelectionEnabled(false);
        setSelectionBackground(new Color(82, 150, 206));

        // 设置表头样式
        JTableHeader header = getTableHeader();
        header.setBackground(Color.DARK_GRAY);
        header.setForeground(Color.WHITE);
        header.setFont(new Font("宋体", Font.BOLD, 14));

        setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                // 根据需要设置文字样式
                if (value != null && value.toString().matches(".*[\\u4E00-\\u9FA5]+.*")) {
                    c.setFont(new Font("宋体", Font.PLAIN, 12)); // 如果包含中文字符，使用宋体
                } else {
                    c.setFont(new Font("Arial", Font.PLAIN, 12)); // 否则使用Arial
                }
                return c;
            }
        });
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component component = super.prepareRenderer(renderer, row, column);

        if (isRowSelected(row)) {
            component.setBackground(getSelectionBackground());
        } else {
            if (row % 2 == 0) {
                component.setBackground(Color.LIGHT_GRAY); // 设置偶数行的背景色为灰色
            } else {
                component.setBackground(Color.WHITE); // 设置奇数行的背景色为白色
            }
        }

        return component;
    }

    static class ElementUITableUI extends BasicTableUI {
        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            JTable table = (JTable) c;
            table.setShowGrid(false);
            table.setIntercellSpacing(new Dimension(0, 0));
            table.setSelectionForeground(Color.BLACK);
            table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                    if (isSelected) {
                        c.setBackground(new Color(173, 216, 230)); // 设置选中行的背景色
                    } else {
                        if (row % 2 == 0) {
                            c.setBackground(new Color(245, 245, 245)); // 设置偶数行的背景色
                        } else {
                            c.setBackground(Color.WHITE); // 设置奇数行的背景色
                        }
                    }
                    return c;
                }
            });
        }
    }
}
